<!--
 * @Descripttion: 问卷调查
 * @Author: xiao li
 * @Date: 2021-01-25 14:09:59
 * @LastEditors: xiao li
 * @LastEditTime: 2021-04-29 14:08:35
-->
<template>
    <div class="lb-questionnaire-list">
        <top-nav></top-nav>
        <div class="page-main">

         <el-row class="page-top-operate">
            <lb-button type='primary' icon='el-icon-plus' @click="$router.push(`/app/questionnaire/edit`)">{{$t('menu.QuestionNaireAdd')}}</lb-button>
          </el-row>
          <el-table
                v-loading='loading'
                :data="tableData"
          :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
                style="width: 100%">
                <el-table-column
                prop="id"
                label="ID">
                </el-table-column>
                <el-table-column
                  label="封面图">
                  <template slot-scope="scope">
                    <lb-image  :src="scope.row.cover" />
                  </template>
                </el-table-column>
                <el-table-column
                prop="title"
                label="问卷标题">
                </el-table-column>
                <el-table-column
                prop="recovery"
                label="收集份数">
                </el-table-column>
                <el-table-column
                prop="top"
                label="排序值">
                </el-table-column>
                <el-table-column
                prop="create_time"
                label="创建时间">
                    <template slot-scope="scope">
                      <p>{{scope.row.create_time | handleTime(1)}}</p>
                      <p>{{scope.row.create_time | handleTime(2)}}</p>
                    </template>
                </el-table-column>
                <el-table-column
                prop="status"
                label="是否上架">
                  <template slot-scope="scope">
                    <el-switch
                      v-model="scope.row.status"
                      :active-value="1"
                      :inactive-value="0"
                      @change="updateItem(scope.row.id,scope.row.status)"
                      >
                    </el-switch>
                  </template>
                </el-table-column>
                <el-table-column
                label="操作">
                    <template slot-scope="scope">
                        <div class="table-operate">
                            <lb-button size='mini' plain type='primary' @click="$router.push(`/app/questionnaire/edit?id=${scope.row.id}`)">{{$t('action.edit')}}</lb-button>
                            <lb-button size='mini' plain type='danger' @click="toDel(scope.row.id)">{{$t('action.delete')}}</lb-button>
                            <lb-button size='mini' plain type='success' @click="$router.push(`/app/questionnaire/data?id=${scope.row.id}`)">{{$t('action.viewData')}}</lb-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <lb-page
            :batch='false'
            :page='searchForm.page'
            :pageSize='searchForm.limit'
            :total='total'
            @handleSizeChange='handleSizeChange'
            @handleCurrentChange='handleCurrentChange'
            >
            </lb-page>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      searchForm: {
        page: 1,
        limit: 10
      },
      total: 0,
      tableData: []
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    getTableDataList () {
      this.loading = true
      this.$api.appointmentQuestionList(this.searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    /**
     * @method: 上下架
     * @param {*} id
     * @param {*} status
     */
    updateItem (id, status) {
      this.$api.appointmentQuestionUpdate({id, status}).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          if (status !== -1) return
          this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
          this.getTableDataList()
        } else {
          if (status === -1) return
          this.getTableDataList()
        }
      })
    },
    /**
     * @method: 删除
     * @param {*} id
     */
    toDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updateItem(id, -1)
      }).catch(() => {

      })
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
    .lb-questionnaire-list{
        width: 100%;
        .page-main{
            width: 100%;
        }
    }
</style>
